<?php

namespace App\Repository\Zhuandan;

use App\Models\Zhuandan\GoodsZModel as model;
use App\Repository\BaseRepository;
use Illuminate\Support\Facades\DB;
use App\Repository\UploadRepository;
use App\Repository\Product\ProductRepository;
use App\Repository\Admin\AdminUsersRepository;
use App\Repository\Orders\OrdersRepository;
use App\Repository\Shop\SuppilerRepository;

class GoodsZRepository extends  BaseRepository
{
  public function __construct()
  {
    $this->model = new model();
    $this->productModel = new ProductRepository();
    $this->userModel = new AdminUsersRepository();
    $this->orderModel = new OrdersRepository();
    $this->suppiler = new SuppilerRepository();

    parent::__construct();
  }
  public function index($where)
  {
    // $cron['where']['shop_id'] = $request->user->shop_id;
    $cron = $where;
    if (empty($cron['order'])) {
      $cron['order'] = ['orderby' => 'id', 'orderdir' => 'desc'];
    }
    $data =  $this->search($this->model, $cron, 1, 10);
    return $data;
  }
  //添加
  public function add($params)
  {
    // 查询商品信息
    $data = $params->all();
    if (!empty($data['goods'])) {
      $ysGoods = $this->productModel->getOne(['id' => $data['goods']]);
      // var_dump($oneGoods);
      if ($ysGoods) {
        $data['ys_product_id'] = $ysGoods->id;
        $data['ys_product_code'] = $ysGoods->product_code;
        $data['ys_product_name'] = $ysGoods->name;
      }
    }
    if (!empty($data['one_goods'])) {
      $oneGoods = $this->productModel->getOne(['id' => $data['one_goods']]);
      // var_dump($oneGoods);
      if ($oneGoods) {
        $data['product_id'] = $oneGoods->id;
        $data['product_code'] = $oneGoods->product_code;
        $data['product_name'] = $oneGoods->name;
      }
    }
    if (!empty($data['two_goods'])) {
      $TwoGoods = $this->productModel->getOne(['id' => $data['two_goods']]);
      if ($TwoGoods) {
        $data['two_product_id'] = $TwoGoods->id;
        $data['two_product_code'] = $TwoGoods->product_code;
        $data['two_product_name'] = $TwoGoods->name;
      }
    }
    // 订单状态显示
    if (!empty($data['order_status'])) {
      $data['order_status_desc'] = ',';
      foreach ($data['order_status'] as $key => $v) {
        $data['order_status_desc'] .= $this->orderModel->getOneOrderStatus($v) . ',';
      }
      $data['order_status'] = implode(',', $data['order_status']);
    }
    if (!empty($data['dist_black'])) {
      $data['dist_black_name'] = ',';
      $dist = ',';
      foreach ($data['dist_black'] as  $key => $v) {
        $resDis = $this->suppiler->getOne([['supplier_id', '=', $data['user']['shop_id']], ['shop_id', '=', $v]]);
        // var_dump($resDis);
        if (!empty($resDis)) {
          $data['dist_black_name'] .= $resDis->name . ',';
          $dist .= $v . ',';
        }
        $data['dist_black'] = $dist;
      }
    } else {
      $data['dist_black'] = '';
    }

    //必填 字段
    $mustField = [
      'product_id',
      'two_product_id'
    ];
    $optionalFields = $this->model->getTableField();
    $msgField = [
      'product_id' => '转单前商品不能为空',
      'two_prduct_id' => '转单后商品不能为空',
    ];
    $insertData  = $this->getRealData($data, $mustField, $optionalFields, $msgField);
    if (!empty($insertData) && is_string($insertData)) {
      //返回错误参数
      return $this->returnMsg(
        2,
        $insertData
      );
    }
    // $isTrue = $this->model->where(['shop_id' => $insertData['shop_id'], 'name' => $insertData['name']])->first();
    // if ($isTrue) {
    //   $res = $this->model->where(['shop_id' => $insertData['shop_id'], 'name' => $insertData['name']])->update($insertData);
    //   if ($res) {
    //     return $this->returnMsg(200, '修改成功');
    //   }
    //   return $this->returnMsg(200, '修改失败,请刷新重新修改');
    // }
    $insertData['created_at'] = date('Y-m-d H:i:s');
    // $insertData['status'] = 1;
    // 查询是否存在 如果存在 直接流转
    $insertData['created_by_name'] = $data['user']->user_name;
    $insertData['shop_id'] = $data['user']->shop_id;

    $res = $this->model->insert($insertData);
    if ($res) {
      return $this->returnMsg(200, '添加成功');
    }
    return $this->returnMsg(1, '添加失败');
  }
  //删除
  public function del($where)
  {
    $flag = true;
    $msg = '未知错误';
    $code = 1;
    if (empty($where)) {
      $flag = false;
      $msg = '删除条件不能为空';
      $code = 2;
    }
    if ($flag) {
      $res = $this->model->where($where)->get();
      if (!$res) {
        $flag = false;
        $msg = '未查询到数据';
        $code = 3;
      }
    }
    if ($flag) {
      $del = $this->model->where($where)->delete();
      if ($del) {
        $msg = '删除成功';
        $code = 200;
      }
    }
    return $this->returnMsg($code, $msg);
  }
  //修改
  public function edit($where, $params, $isMust = false)
  {
    if (empty($where)) {
      return $this->returnMsg(1, '条件不能为空');
    }
    if (empty($params)) {
      return $this->returnMsg(2, '修改参数不能为空');
    }
    $data = $params->all();
    if (!empty($data['goods'])) {
      $oneGoods = $this->productModel->getOne(['id' => $data['goods']]);
      // var_dump($oneGoods);
      if ($oneGoods) {
        $data['ys_product_id'] = $oneGoods->id;
        $data['ys_product_code'] = $oneGoods->product_code;
        $data['ys_product_name'] = $oneGoods->name;
      }
    }
    if (!empty($data['one_goods'])) {
      $oneGoods = $this->productModel->getOne(['id' => $data['one_goods']]);
      // var_dump($oneGoods);
      if ($oneGoods) {
        $data['product_id'] = $oneGoods->id;
        $data['product_code'] = $oneGoods->product_code;
        $data['product_name'] = $oneGoods->name;
      }
    }
    if (!empty($data['two_goods'])) {
      $TwoGoods = $this->productModel->getOne(['id' => $data['two_goods']]);
      if ($TwoGoods) {
        $data['two_product_id'] = $TwoGoods->id;
        $data['two_product_code'] = $TwoGoods->product_code;
        $data['two_product_name'] = $TwoGoods->name;
      }
    }
    // 订单状态显示
    if (!empty($data['order_status'])) {
      $data['order_status_desc'] = ',';
      foreach ($data['order_status'] as $key => $v) {
        $data['order_status_desc'] .= $this->orderModel->getOneOrderStatus($v) . ',';
      }
      $data['order_status'] = implode(',', $data['order_status']);
    }
    if (!empty($data['dist_black'])) {
      $data['dist_black_name'] = ',';
      $dist = ',';
      foreach ($data['dist_black'] as  $key => $v) {
        $resDis = $this->suppiler->getOne([['supplier_id', '=', $data['user']['shop_id']], ['shop_id', '=', $v]]);
        // var_dump($resDis);
        if (!empty($resDis)) {
          $data['dist_black_name'] .= $resDis->name . ',';
          $dist .= $v . ',';
        }
        $data['dist_black'] = $dist;
      }
    }
    //过滤字段
    $optionalFields = $this->model->getTableField();
    $field  = $this->getRealData($data, [], $optionalFields, []);
    $isTrue = $this->model->where($where)->first();
    if (!$isTrue) {
      return $this->returnMsg(100, '未查询到修改的数据');
    }
    //先查询是否存在
    $res = $this->model->where($where)->update($field);
    if ($isMust && $res) {

      return $this->returnMsg(200, '修改成功');
    }
    if (!$isMust) {
      return $this->returnMsg(200, '修改成功');
    }
    return $this->returnMsg(3, '请重新修改');
  }
  //查看  存入缓存
  public function sel($where = [], $page = 1, $limit = 10)
  {
    $res = $this->search($this->model, $where, $page, $limit);
    return $res;
  }
  //查询 单条数据
  public function oneSel($where)
  {
    return $this->model->where($where)->first();
  }
  public function getOne($where, $field = ['*'])
  {
    return $this->model->where($where)->select($field)->orderBy('id', 'desc')->first();
  }
  public function getAll($where, $field = ['*'])
  {
    return $this->model->where($where)->select($field)->get();
  }
}
